﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FILSTARTUPS_CMS.Models;
using FILSTARTUPS_CMS.Helpers;
using System.Data;

namespace FILSTARTUPS_CMS.Controllers
{
    [AdminAccess]
    public class ForumController : Controller
    {
        //
        // GET: /Forum/
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();

        public ActionResult Index()
        {
            var forum = (from f in cms_dbconnect.Forums
                         join r in cms_dbconnect.RoutingType
                         on f.routingidFK equals r.routingidPK
                         orderby f.forumidPK descending
                         select new ForumGrid
                         {
                             datecreated = f.datecreated,
                             forumidPK = f.forumidPK,
                             forumtitle = f.forumtitle,
                             viewtype = r.routingname
                         }).ToList();

            return View(forum);
        }

        public ActionResult Create()
        {
            ViewData["routing"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
            return View();
        }

        [HttpPost]
        public ActionResult Create(Forum forum, FormCollection form)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    
                    forum.routingidFK = Convert.ToInt32(form["routingidFK"].Trim());
                    forum.datecreated = DateTime.Now;
                    cms_dbconnect.Forums.Add(forum);
                    cms_dbconnect.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View(forum);
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            var forum = cms_dbconnect.Forums.Where(f => f.forumidPK.Equals(id)).SingleOrDefault();
            ViewData["routing"] = cms_dbconnect.RoutingType.OrderBy(r => r.routingidPK).ToList();
            return View(forum);
        }

        [HttpPost]
        public ActionResult Edit(Forum forum, FormCollection form)
        {
            int forumid = Convert.ToInt32(form["forumidPK"].Trim());

           
            try
            {
                forum.datecreated =DateTime.Now;
                forum.routingidFK = Convert.ToInt32(form["routingidFK"].Trim());
                cms_dbconnect.Entry(forum).State = EntityState.Modified;
                cms_dbconnect.SaveChanges();
              //  return View();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View(ex);
            }
          
        }

        public ActionResult Details(int id)
        {
            try
            {
                var forum = (from f in cms_dbconnect.Forums
                             join r in cms_dbconnect.RoutingType
                                 on f.routingidFK equals r.routingidPK
                             where f.forumidPK.Equals(id)
                             select new ForumDetails
                             {
                                 forumdescription = f.forumdescription,
                                 forumidPK = f.forumidPK,
                                 forumtitle = f.forumtitle,
                                 viewtype = r.routingname
                             }).SingleOrDefault();
                if (forum != null)
                {
                    return View(forum);
                }
            }
            catch (Exception ex)
            {
                return View(ex);
            }
            return View();
        }

    }
}
